package org.edu.modules.oa.controller;


import com.aliyun.oss.OSS;
import groovy.util.logging.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.RedisUtil;
import org.edu.modules.oa.entity.VeOaDocumentEntity;
import org.edu.modules.oa.entity.VeOaHuishouzhanEntity;
import org.edu.modules.oa.service.IVeOaHuishouzhanService;
import org.edu.modules.oa.service.IVeOaPersonDocumentService;
import org.edu.modules.oa.vo.VeOaDocumentVo;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Api(tags="知识文档-回收站")
@RestController
@RequestMapping("/oa/VeOaHuishouzhan")
@Slf4j
public class VeOaHuishouzhanController {

    @Autowired
    private RedisUtil redisUtil; // redis服务类
    @Resource
    private OSS ossClient;
    @Resource
    private IVeOaPersonDocumentService iVeOaPersonDocumentService;
    @Resource
    private IVeOaHuishouzhanService iVeOaHuishouzhanService;

    //条件查询 进入下一级
    @AutoLog(value = "知识文档-回收站")
    @ApiOperation(value="条件查询", notes="个人文件-回收站")
    @PostMapping(value = "/searchTable")
    public Result<?> searchTable(VeOaHuishouzhanEntity veOaHuishouzhanEntity, Integer startPage, Integer pageSize){

        veOaHuishouzhanEntity.setIsDeleted(1);
        //veOaDocumentEntity.setDocType("个人");
        HashMap<String,Object> map = iVeOaHuishouzhanService.selectDocument(veOaHuishouzhanEntity,startPage,pageSize);
        return Result.OK(map);
    }

    //条件查询 返回上一级
    @AutoLog(value = "知识文档-回收站")
    @ApiOperation(value="返回上一级", notes="回收站-返回上一级")
    @PostMapping(value = "/backToUplevel")
    public Result<?> backToUplevel(VeOaHuishouzhanEntity veOaHuishouzhanEntity,Integer startPage,Integer pageSize){
        Integer isDelete = 1;
        String docType = "";
        HashMap<String,Object> map = iVeOaHuishouzhanService.backToUpLevel(veOaHuishouzhanEntity,startPage,pageSize,isDelete,docType);
        if (map.size()<=0){
            return Result.error(500,"无上一级");
        }

        return Result.OK(map);
    }


    //删除选中
    @AutoLog(value = "知识文档-回收站")
    @ApiOperation(value = "删除选中", notes = "个人文件-删除选中")
    @GetMapping(value = "/deleteAll")
    public Result<?> deleteAll(@RequestParam List<String> ids){
        System.out.println("删除的数组"+ids);
        List<Long> idList = new ArrayList<>();
        for (int i=0;i<ids.size();i++){
            System.out.println(ids.get(i));
            idList.add(Long.parseLong(ids.get(i)));
        }

        Integer res = 0;
        for (int i=0;i<idList.size();i++){
            res = iVeOaHuishouzhanService.deleteTrueDocument(idList.get(i));
        }
        if (res == 0){
            return Result.error(500,"删除过程中出现错误");
        }

        return Result.OK();
    }


    //导出表格
    @AutoLog(value = "知识文档-回收站")
    @ApiOperation(value = "导出excel表格", notes = "回收站-导出excel表格")
    @GetMapping(value = "/exportHuishouzhanTable")
    //ModelAndView 导出表格流
    public ModelAndView exportHuishouzhanTable(VeOaHuishouzhanEntity veOaHuishouzhanEntity,Integer startPage,Integer pageSize){
        veOaHuishouzhanEntity.setIsDeleted(1);
        //获取登录人信息
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //查当前位置的数据
        List<VeOaDocumentVo> voList = iVeOaHuishouzhanService.export(veOaHuishouzhanEntity,startPage,pageSize);
        //创建导出流
        ModelAndView modelAndView = new ModelAndView(new JeecgEntityExcelView());
        //文件的文件名
        modelAndView.addObject(NormalExcelConstants.FILE_NAME,"回收站表");
        //数据的类型
        modelAndView.addObject(NormalExcelConstants.CLASS, VeOaDocumentVo.class);
        // ExportParams  Export工具类   （参数1--》title:表格标题  参数2--》secomdTitle:第二行的标题，  参数3--》secomdTitle:最下面尾部的信息）
        ExportParams exportParams = new ExportParams("回收站表","导出人:"+user.getRealname(),"回收站表");
        modelAndView.addObject(NormalExcelConstants.PARAMS,exportParams);
        modelAndView.addObject(NormalExcelConstants.DATA_LIST,voList);

        return modelAndView;
    }


    //恢复文件/文件夹
    @AutoLog(value = "知识文档-回收站")
    @ApiOperation(value = "恢复", notes = "回收站-恢复")
    @GetMapping(value = "/recover")
    public Result<?> recover(@RequestParam List<String> ids){
        System.out.println("恢复的数组"+ids);
        List<Long> idList = new ArrayList<>();
        for (int i=0;i<ids.size();i++){
            System.out.println(ids.get(i));
            idList.add(Long.parseLong(ids.get(i)));
        }

        //先拿idList中的每个上级路径
        List<VeOaHuishouzhanEntity> parIdList = new ArrayList<>();
        Integer checkResult = 0;
        for (int i=0;i<idList.size();i++){
            VeOaHuishouzhanEntity entity = new VeOaHuishouzhanEntity();
            entity.setId(idList.get(i));
            VeOaHuishouzhanEntity veOaHuishouzhanEntity = iVeOaHuishouzhanService.findParentId(entity);
            parIdList.add(veOaHuishouzhanEntity);

        }
        //恢复前判断是否能恢复 找到能恢复的上级的id
        List<VeOaHuishouzhanEntity> recIdList = new ArrayList<>();
        for (int i=0;i<parIdList.size();i++){
             Long id = iVeOaHuishouzhanService.findRecoverIdIsTheirParentId(parIdList.get(i).getId(),parIdList.get(i).getParentId());
             if (id == -1L || id == null){
                 return Result.error(500,"存在同名文件或文件夹，不可恢复");
             }
             VeOaHuishouzhanEntity veOaHuishouzhanEntity = new VeOaHuishouzhanEntity();
             veOaHuishouzhanEntity.setId(parIdList.get(i).getId());
             veOaHuishouzhanEntity.setParentId(Integer.parseInt(String.valueOf(id)));
             recIdList.add(veOaHuishouzhanEntity);
        }
        //改 isDelete为0
        Boolean flagRecover = true;
        Integer resR = 0;
        for (int i=0;i<recIdList.size();i++){
            resR = iVeOaHuishouzhanService.recoverFileFolder(recIdList.get(i).getId(),recIdList.get(i).getParentId());
        }
        if (resR == 0){
            return Result.error(500,"恢复失败");
        }
        //改传过来的ids集合的parentId
        Boolean flagChangeParId = true;
        Integer resC = 0;
        for (int i=0;i<recIdList.size();i++){
            resC = iVeOaHuishouzhanService.recoverToItsId(recIdList.get(i).getId(),recIdList.get(i).getParentId());

        }
        if (resC == 0){
            return Result.error(500,"恢复失败");
        }

        return Result.OK();
    }


}
